package matching;

import java.util.ArrayList;

import data.RTEData;

import parser.RTEXmlParser;

public class WordMatch extends Matcher {	
	public WordMatch(String task, double threshold) {
		super(task, threshold);
	}

	@Override
	public void match() {
		numberOfTextCases = RTEData.RTEDataList.size();
		
		for (RTEData rteData : RTEData.RTEDataList) {
			int matches = 0;
			float idfValue = 0;
			boolean entails = false;
	
			//iterate over each word in rteData.getHypothesis() and check if that word exists in rteData.getText()
			String[] hypothesisWords = rteData.getHypothesis().split(" ");
			String[] textWords = rteData.getText().split(" ");

			for (int i = 0; i < hypothesisWords.length; i++) {
				for (int j = 0; j < textWords.length; j++) {
					if(hypothesisWords[i].equals(textWords[j]) && hypothesisWords[i] != ""){
						matches++;
						
						idfValue += idfNormal.get(hypothesisWords[i]);
						//avoid matching the same word twice
						textWords[j] = ""; 
						hypothesisWords[i] = "";
					} 					
				}
			}
			
			rteData.setWordMatchPercentage(matches / (float)hypothesisWords.length);
			
			//check for entailment.  
//			float wordMatch = matches/(float)hypothesisWords.length;
			float idfHypSum = 0;
			for(int i = 0; i < hypothesisWords.length; i++)  {
				idfHypSum += idfNormal.get(hypothesisWords[i]); 
			}
			
			float idfWordMatch = idfValue / idfHypSum;
			
			if(idfWordMatch >= threshold) {
				entails = true;
			}
						
//			if(wordMatch >= threshold) { 
//				entails = true;
//			}		

			this.addJudgement(rteData.getId(), entails);
		
			if(entails == rteData.isEntailment()) { 
				entailments++;
			}
			else
				errors.add(rteData.getId());
		}
		correctness = (float)entailments/(float)numberOfTextCases;

		System.out.println(this);
		RTEXmlParser.WritePredictions("WordMatch", this.getJudgementData());
	}
}
